var actionNodeID="";

jQuery(document).ready(function() {
	
	$("#layout1").ligerLayout({ leftWidth: 200,height: '100%',heightDiff:-10,space:4});
	//创建JQGRID表格
	var grid = $("#table1").ligerGrid({
        columns: [
                  { display: '收款单编号', name: 'skCode', width: 160 },
                  { display: '销售出货单号', name: 'oriCode', width: 160 },
                  { display: '客户编号', name: 'spCode', width: 160 },
                  { display: '客户名称', name: 'spName', width: 160 },
                  { display: '单据状态', name: 'billStatus', width:100,
                	  render: function (item) {
                		  var billstatus=item.billStatus;
                		  if(billstatus=="0"){
                			  return "草稿";
                		  }
                		  if(billstatus=="2"){
                			  return "完成";
                		  }
                	  }},
                  { display: '收款状态', name: 'skState', width: 100,
                	  render: function (item){               		 
                		  var skstate=item.skState;
                		  if(skstate=="0"){
                			  return "未收款";
                		  }
                		  if(skstate=="1"){
                			  return "部分收款";
                		  }
                		  if(skstate=="2"){
                			  return "全部收款";
                		  }
                	  }},
                  { display: '收款时间', name: 'yfDate', width: 160,
                		  render: function (item) {
                			  var yfdate=new Date(item.yfDate);
                			  return formatDate(yfdate,"yyyy-MM-dd");
                		  }},                 
                  { display:'汇率', name:'exchange_rate', width:100},
                  { display: '币种', name:'currency', width: 100},
                  { display: '总货款(含税)', name: 'money', width: 100,type: 'float',render: function(item){
                	  if(item.money == null || item.money == "" || parseFloat(item.money) == 0){
                		  return "0.0";
                	  }else{
                		  return (item.money*1).toFixed(4);
                	  }
                  }},
                  { display: '已收款(含税)', name: 'yshk', width: 100,type: 'float',render: function(item){
                	  if(item.yshk == null || item.yshk == "" || parseFloat(item.yshk) == 0){
                		  return "0.0";
                	  }else{
                		  return (item.yshk*1).toFixed(4);
                	  }
                  } },
                  { display: '制单人', name: 'createName', width: 150},
                  { display: '创建日期', name: 'dctime', width: 100,
                	  render: function (item){
                		  var cdate=new Date(item.dctime);
                		  return formatDate(cdate,"yyyy-MM-dd");
                	  }},
                
                  { display: '备注', name: 'remark', width: 150 }
                 ],  
         usePager: true,
         pageSize:5,
         pageSizeOptions :[5,10],
         url : basic_Url+"/list.do?billStatus=0",
         width: "100%",
         height:"60%",
         checkbox: true,
         enabledSort:false,
         onDblClickRow : function (data, rowindex, rowobj){
        	 basic_Title='编辑';
        	 edit(data);
         },
         toolbar: { items: [
							{ text: '新增草稿', click: addBill, icon: 'add' },
							{ line: true },
                            { text: '编辑提交', click: commitBill, icon: 'modify' },
                            { line: true },
                            { text: '删除', click: delBill, img: ctx+'/js/ligerUI/skins/icons/delete.gif' }
                           ]
        }
    });
	//创建JQGRID表格
	var grid1 = $("#table2").ligerGrid({
        columns: [
                  { display: '收款单编号', name: 'skCode', width: 160 },
                  { display: '销售出货单号', name: 'oriCode', width: 160 },
                  { display: '客户编号', name: 'spCode', width: 160},
                  { display: '客户名称', name: 'spName', width: 160 },
                  { display: '单据状态', name: 'billStatus', width:100,
                	  render: function (item) {
                		  var billstatus=item.billStatus;
                		  if(billstatus=="0"){
                			  return "草稿";
                		  }
                		  if(billstatus=="2"){
                			  return "完成";
                		  }
                	  }},
                  { display: '收款状态', name: 'skState', width: 100,
                		  render: function (item){               		 
                    		  var skstate=item.skState;
                    		  if(skstate=="0"){
                    			  return "未收款";
                    		  }
                    		  if(skstate=="1"){
                    			  return "部分收款";
                    		  }
                    		  if(skstate=="2"){
                    			  return "全部收款";
                    		  }
                	  }},                  
                  { display: '收款时间', name: 'yfDate', width: 160,
                		  render: function (item) {
                			  var yfdate=new Date(item.yfDate);
                			  return formatDate(yfdate,"yyyy-MM-dd");
                		  }}, 
                  { display:'汇率', name:'exchange_rate', width:100},
                  { display: '币种', name:'currency', width: 100},
                  { display: '总货款(含税)', name: 'money', width: 100,type: 'float',render: function(item){
                	  if(item.money == null || item.money == "" || parseFloat(item.money) == 0){
                		  return "0.0";
                	  }else{
                		  return (item.money*1).toFixed(4);
                	  }
                  }},
                  { display: '已收款(含税)', name: 'yshk', width: 100,type: 'float',render: function(item){
                	  if(item.yshk == null || item.yshk == "" || parseFloat(item.yshk) == 0){
                		  return "0.0";
                	  }else{
                		  return (item.yshk*1).toFixed(4);
                	  }
                  }},
                  { display: '制单人', name: 'createName', width: 150},
                  { display: '创建日期', name: 'dctime', width: 100,
                	  render: function (item){
                		  var cdate=new Date(item.dctime);
                		  return formatDate(cdate,"yyyy-MM-dd");
                	  }},
                
                  { display: '备注', name: 'remark', width: 150 }
                 ],  
         usePager: true,
         pageSize:4,
         pageSizeOptions :[4,10],
         rownumbers:true,
         url : basic_Url+"/list.do?billStatus=5",
         width: "100%",
         height:"40%",
         checkbox: false,
         enabledSort:false,
         onDblClickRow : function (data, rowindex, rowobj){
        	 basic_Title='查看';
        	 edit(data);
         }
    });
		
	//左侧树
	var _tmpParam = {_search:'true',filters:'{"groupOp":"AND","rules":[{"field":"parentname","op":"eq","data":"客户"}]}'};
	var lefttree1 = $("#tree1").ligerTree({  
		 nodeWidth: 115,
		 textFieldName:"name",
		 checkbox: false,
         slide: false,
         onClick:function(node){
        	 actionNodeID = node.data.code;        	 
        	 multipleSearch("#table1",true,"",basic_Url+"/list.do?billStatus=0&spCode="+actionNodeID);
        	 multipleSearch("#table2",true,"",basic_Url+"/list.do?billStatus=5&spCode="+actionNodeID);
         }
    });
	lefttree1.loadData(null, basic_Url+'/tree.do', _tmpParam ) ;
		
	$("#pageloading").hide();
	
});

//添加收款单(草稿)
function addBill(){
 	if(actionNodeID == null || actionNodeID.length<=0){
 		$.dialog.alert("请选择客户");
 		return false;
 	}
	var url = basic_WUrl+"/operate/add/"+actionNodeID+".do";
	ShowIframeDialog("operate","添加",url,show_Width,show_Hight);
}

//提交草稿，确认收款
function commitBill(){
	 var grid = $("#table1").ligerGetGridManager(); 
		var rows = grid.getCheckedRows();
		var data =grid.getSelectedRow();
		if(rows.length==0){
			$.dialog.alert("请选择一行数据!");
			return false;
		}else{
			basic_Title='提交';
			edit(data);
		}
}

//删除收款单
function delBill(){
	var manager = $("#table1").ligerGetGridManager(); 
	var rows =manager.getSelectedRows();
	var bol = false;
	if (rows==null || 0 == rows.length){
			$.dialog.alert('请选择需要操作删除的行！');
			return;
	}
	for(var i=0;i<rows.length;i++){
	 	if (rows[i].createName != user){
	 		bol = true;
		}
	}
	if(bol){
		$.dialog.alert('请选择自己创建的草稿单据进行删除操作!');
		return;
	}else{
		bol = false;
		for(var i=0;i<rows.length;i++){
		 	if (rows[i].billStatus>0){
		 		bol = true;
			}
		}
		if(bol){
			$.dialog.alert('非草稿状态的单据不能删除!');
			return;
		}else{
			del();
			bol = false;
		}
	}	
}
